package com.atguigu.gmall.mq.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/9/4 10:41
 * @Version 1.0
 */
@RestController
@RequestMapping("mq")
public class MqController {


    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg")
    public Result sendMsg(){
        //  发送消息...
        rabbitService.sendMsg("exchange.confirm","routing.confirm","死鬼，才回来....");
        return Result.ok();
    }

    //  发送延迟消息：
    @GetMapping("sendDeadMsg")
    public Result sendDeadMsg(){
        //  获取到当前系统时间 多线程：LocalDate LocalTime LocalDateTime , 单线程Date  Instant | Date
        Instant instant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        System.out.println(date);
        //  订单： LocalDate  LocalDateTime
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(LocalDateTime.now().format(dateTimeFormatter));
        //  发送消息...
        rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"atguigu");
        return Result.ok();
    }

    //  发送延迟消息：
    @GetMapping("sendDelayMsg")
    public Result sendDelayMsg(){
        //  获取到当前系统时间 多线程：LocalDate LocalTime LocalDateTime , 单线程Date  Instant | Date
        Instant instant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
        Date date = Date.from(instant);
        System.out.println(date);
        //  订单： LocalDate  LocalDateTime
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //  发送消息...
        //  rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"atguigu");
        //  需要使用原生的模版
        //        this.rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,"ok",
        //                message -> {
        //                    System.out.println(LocalDateTime.now().format(dateTimeFormatter));
        //                    //  设置延迟时间
        //                    message.getMessageProperties().setDelay(10000);
        //                    //  返回
        //                    return message;
        //                });

        //  调用发送延迟消息方法。
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content","ok");
        jsonObject.put("uuid", UUID.randomUUID().toString());
        this.rabbitService.sendDelayMsg(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,jsonObject.toJSONString(),10);
        return Result.ok();
    }
}
